<?php
$bool_IE 	= false;

if( strpos( $_SERVER["HTTP_USER_AGENT"], "MSIE" ) !== false ) $bool_IE = true;
?>

<div>
    <div class="border_box">     

        <form class="form_input" action="" method="post" id="form_item" name="form_item">
            <fieldset class="border_radius_all">
                <legend></legend>

                <div class="form_input_row">
					<label class="label_title">Đường Dẫn Liên Kết:*</label>
					<input
						id			= "txt_url"
						name		= "txt_url"
						title		= "Đường Dẫn Đến Liên Kết"
						type		= "text"
						value		= "<?php if(isset($o_model)) { echo $o_model->url;}?>"
						class		= "form_textbox textbox_css3"
						tabindex	= "1"
						onblur		= "validation_onblur(this,'lbl_url_validation')"
					/>
					<label
						id		= "lbl_url_validation" 
						class	= "lable_validation"
					>Bạn chưa nhập đường dẫn liên kết.</label>
				</div><!--End form_input_row Name-->
				
                <div class="form_input_row">
					<label class="label_title">Tên Đường Dẫn Liên Kết:*</label>
					<input
						id			= "txt_title"
						name		= "txt_title"
						title		= "Tên Đường Dẫn Liên Kết"
						type		= "text"
						value		= "<?php if(isset($o_model)) { echo $o_model->title;}?>"
						class		= "form_textbox textbox_css3"
						tabindex	= "2"
						onblur		= "validation_onblur(this,'lbl_title_validation')"
					/>
					<label
						id		= "lbl_title_validation"
						class	= "lable_validation"
					>Bạn chưa nhập tên đường dẫn liên kết.</label>
				</div><!--End form_input_row Name-->
								
                <div class="form_input_row">
					<label class="label_title">Gợi Ý:</label>
					<input
						id			= "txt_tooltip"
						name		= "txt_tooltip"
						title		= "Gợi Ý"
						type		= "text"
						value		= "<?php if(isset($o_model)) { echo $o_model->tooltip;}?>"
						class		= "form_textbox textbox_css3"
						tabindex	= "3"
					/>
				</div>
				
                <div class="form_input_row" style="border: none;">
					<div class="input_items_horizon">
						<input
							title		= "gửi yêu cầu"
							name		= "btn_save"
							id			= "btn_save"
							type		= "submit"
							value		= "Lưu"
							class		= "input_button button_css3"
							tabindex	= "4"
						/>
                        <input
							title		= "gửi yêu cầu"
							name		= "btn_cancel"
							id			= "btn_cancel"
							type		= "button"
							value		= "Hủy Bỏ"
							class		= "input_button button_css3"
							tabindex	= "5"
						/>
					</div><!--End input_items_horizon-->
				</div><!--End form_input_row button-->

            </fieldset>
        </form>

    </div><!--End box_950-->
</div><!--End container_960-->

<script type="text/javascript">

$(document).ready(function () {
	$("#txt_url").focus();
});
						
$('#btn_cancel').click(function() {

	$("#dialog_notice").dialog("close");

	var i_key 	= <?=$i_key ?>;
	if(i_key != 0)
		$("#tr_"+i_key).css("background-color","#FFFFFF");
});

function validation_onblur( element, lbl_validation) {
	
	if( element.value == ""){
		$('#'+lbl_validation).fadeIn("slow", "linear");
		//$(element).focus();
	} else {
		$('#'+lbl_validation).fadeOut();
	}
}

function form_onsubmit(param)
{
	if ( $("#txt_title").val() == "" || $("#txt_title").val().length < 4 )
	{
		$("#lbl_title_validation").fadeIn("slow", "linear");
		$("#txt_title").focus();
		return false;
	}

	if ( $("#txt_url").val() == "" || $("#txt_url").val().length < 4)
	{
		$("#lbl_url_validation").fadeIn("slow", "linear");
		$("#txt_url").focus();
		return false;
	}
	
	return true;
}

//attach a submit handler to the form
$("#form_item").submit(function(event) {
	
	//stop form from submitting normally
	event.preventDefault();
	
	var bool_submit = form_onsubmit("form_item");
	if (bool_submit == true) {

		$( "#dialog_excute" ).dialog( "open" );
		var i_key = <?=$i_key ?>;

		$.ajax({
			url			: "<?=Yii::app()->createUrl("links/save");?>",
			type		: "POST",
			contentType	: "application/x-www-form-urlencoded; charset=UTF-8",
			cache		: false,
			data		: {
				item_key 		: i_key,
				txt_url			: $("#txt_url").val(),
				txt_title		: $("#txt_title").val(),
				txt_tooltip		: $("#txt_tooltip").val()
			},
			success	: function(string){

				/**
				 * Kiểu mặc định trả về là dạng String, bạn dùng hàm parseJSON để phân tích dữ liệu trả về
				 * có 2 cách parse JSON là : JSON.parse() và $.parseJSON();
				 * 1. var getData = JSON.parse(string);
				 * 2. var getData = $.parseJSON(string);
				**/
				var arr_result = $.parseJSON(string);
				if(arr_result.error != null){
					alert(arr_result.error);
					$("#dialog_excute").dialog( "close" );
					return;
				}

				var ct_row = "";

				if(i_key != 0) {
					
					ct_row += "<td>";
					ct_row += "	<input";
					ct_row += "		id		= 'chk_delete'";
					ct_row += "		name	= 'chk_delete'";
					ct_row += "		type	= 'checkbox'";
					ct_row += "		class	= 'check_box'";
					ct_row += "		value	= '"+ arr_result.key +"'";
					ct_row += "		onclick='display_button(this);'";
					ct_row += "	/>";
					ct_row += "</td>";
					
					ct_row += "<td>";
					ct_row += "	<span";
					ct_row += "		class	= 'icon_button icon_edit'";
					ct_row += "		onclick	= \"call_edit_page("+ arr_result.key +");\"";
					ct_row += "	> ";
					ct_row += "	</span>";
					ct_row += "</td>";
									
					ct_row += "<td>" + arr_result.key		+ "</td>";
					ct_row += "<td>" + arr_result.title		+ "</td>";
					ct_row += "<td>" + arr_result.url		+ "</td>";
					ct_row += "<td>" + arr_result.tooltip	+ "</td>";
					ct_row += "<td>" + arr_result.insert	+ "</td>";

					$("#tr_"+i_key).html(ct_row);

					$("#tr_"+i_key).css("background-color","#FFFFFF");

					$("#dialog_excute").dialog( "close" );
					$("#dialog_notice").dialog("close");

				} else if (i_key == 0) {
					
					alert("Thêm Thành Công");
					$("#dialog_excute").dialog( "close" );
					$('#txt_url').focus();
				}
			},
			error: function (){
				alert('Trang hiện hành không tồn tại.');
				$("#dialog_excute").dialog( "close" );
			}
		});//End Ajax
	}//End if (bool_submit == true)
	
});//End form submit
</script>